package com.xsy.android.compose.wandroid.ui.page.home

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.paging.compose.LazyPagingItems
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.logic.model.ArticleModel
import com.xsy.android.compose.wandroid.logic.model.BannerBean
import com.xsy.android.compose.wandroid.logic.model.PlayState
import com.xsy.android.compose.wandroid.ui.page.home.view.HomeContent
import com.xsy.android.compose.wandroid.ui.view.TopAppBar
import com.xsy.android.compose.wandroid.ui.view.lce.LcePage

/**
 *@author: xiaosy
 *时间: 2024/7/4
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@Composable
fun HomePage(
    modifier: Modifier = Modifier,
    bannerData: PlayState<List<BannerBean>>,
    lazyPagingItems: LazyPagingItems<ArticleModel>,
    loadData: () -> Unit,
    toArticleDetails: (ArticleModel) -> Unit
) {
    var loadArticleState by rememberSaveable {
        mutableStateOf(false)
    }
    if (!loadArticleState) {
        loadArticleState = true
        loadData()
    }

    Column(modifier = modifier.fillMaxSize()) {
        TopAppBar(title = stringResource(id = R.string.home_page))
        LcePage(playState = bannerData, onErrorClick = {
            loadData()
        }) { data ->
            loadArticleState = true
            Log.d("HomePage","data->$data")
            HomeContent(
                data = data,
                lazyPagingItems = lazyPagingItems,
                toArticleModel = toArticleDetails
            )
        }
    }
}

@Preview
@Composable
private fun PreviewHomePage() {
//    HomePage()
}